package at.jaki.ubb.crawling.demo.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.log4j.Logger;

import at.jaki.ubb.crawling.demo.model.StarData;

public final class FileUtil {

	private static final Logger LOG = Logger.getLogger(FileUtil.class);

	private FileUtil() {
	}

	public static StarData readStarFromFile(File f) throws FileNotFoundException, IOException, ClassNotFoundException {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
		Object readObject = ois.readObject();
		return (StarData) readObject;
	}

	public static StarData readStarFromFileHandled(File f) {
		try {
			return readStarFromFile(f);
		} catch (FileNotFoundException e) {
			LOG.error(e.getClass().getName() + " " + e.getMessage());
		} catch (IOException e) {
			LOG.error(e.getClass().getName() + " " + e.getMessage());
		} catch (ClassNotFoundException e) {
			LOG.error(e.getClass().getName() + " " + e.getMessage());
		}
		return null;
	}

	public static void writeStarToFile(File f,StarData sd) throws FileNotFoundException, IOException {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
		oos.writeObject(sd);
		oos.flush();
		oos.close();
	}

}
